package com.sun.domain;

import com.alibaba.fastjson.JSON;

public class Result<T> {
    private int code;
    private String message;
    private T data;
    private int dataType;
    private String grid;
    private Boolean isAsync = false;

    public Result() {
    }

    private Result(int code, T data) {
        this.code = code;
        this.data = data;
        this.isAsync = false;
    }

    private Result(int code, String message, T data, int dataType, String grid) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.dataType = dataType;
        this.grid = grid;
        this.isAsync = false;
    }

    private Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.isAsync = false;
    }

    private Result(int code, String message) {
        this.code = code;
        this.message = message;
        this.isAsync = false;
    }

    public Result(int code, String message, T data, int dataType) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.dataType = dataType;
        this.isAsync = false;
    }

    public Result(int code, T data, int dataType) {
        this.code = code;
        this.data = data;
        this.dataType = dataType;
        this.isAsync = false;
    }

    public static Result createByCodeAndMessage(int code, Object data, String message) {
        return new Result(code, message, data);
    }

    public static Result createByCodeAndMessage(int code, String message, Object data, int dataType) {
        return new Result(code, message, data, dataType);
    }

    public static Result createBySuccess() {
        return new Result(ResultCode.SUCCESS.code(), "操作成功");
    }

    public static Result createBySuccessGrid(Object data, String grid, String message) {
        return new Result(ResultCode.SUCCESS.code(), message, data, 1, grid);
    }

    public static Result createBySuccessMessage(String errMsg) {
        return new Result(ResultCode.SUCCESS.code(), errMsg);
    }

    public static Result createBySuccess(Object data) {
        return new Result(ResultCode.SUCCESS.code(), data);
    }

    public static Result createByGrid(Object ans) {
        return new Result(ResultCode.SUCCESS.code(), ans, 1);
    }

    public static Result createBySuccess(String msg, Object data) {
        return new Result(ResultCode.SUCCESS.code(), msg, data);
    }

    public static Result createByError() {
        return new Result(ResultCode.FAIL.code(), "操作失败");
    }

    public static Result createByErrorMessage(String errorMessage) {
        return new Result(ResultCode.FAIL.code(), errorMessage);
    }

    public static Result createByErrorCodeMessage(int errorCode, String errorMessage) {
        return new Result(errorCode, errorMessage);
    }

    public String toString() {
        return JSON.toJSONString(this);
    }

    public Result setCode(ResultCode resultCode) {
        this.code = resultCode.code();
        return this;
    }

    public Result setCustomErrorCode(int errorCode) {
        this.code = -Math.abs(errorCode);
        return this;
    }

    public int getDataType() {
        return this.dataType;
    }

    public Result setDataType(int dataType) {
        this.dataType = dataType;
        return this;
    }

    public Result asJson() {
        this.dataType = 0;
        return this;
    }

    public Result asDataObject() {
        this.dataType = 1;
        return this;
    }

    public Result asGrid() {
        this.dataType = 2;
        return this;
    }

    public String getGrid() {
        return this.grid;
    }

    public void setGrid(String grid) {
        this.grid = grid;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

    public Object getData() {
        return this.data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    public Result setAsync(Boolean async) {
        this.isAsync = async;
        return this;
    }

    public Boolean getAsync() {
        return this.isAsync;
    }
}